﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.Serialization;

namespace Trentacular.Office.Server.Search.FacetedSearch
{
    /// <summary>
    /// Represents a collection of Facet Provider Configuration Properties
    /// </summary>
    [Serializable]
    [CollectionDataContract(
        Name = "Properties",
        Namespace = OfficeServerSearchNamespace.Value
        )]
    public class ConfigurationPropertyCollection : Collection<ConfigurationProperty>
    {
        /// <summary>
        /// Gets or sets the property value with the specified key.
        /// </summary>
        /// <value></value>
        public string this[string key]
        {
            get
            {
                var property = GetProperty(key);
                return (property == null) ? null : property.Value;
            }
            set
            {
                var property = GetProperty(key);
                if (property != null)
                    property.Value = value;
                else
                    this.Add(new ConfigurationProperty { Key = key, Value = value });
            }
        }

        private ConfigurationProperty GetProperty(string key)
        {
            return this.SingleOrDefault(
                p => p.Key.Equals(key, StringComparison.InvariantCultureIgnoreCase)
                );
        }
    }
}
